package cn.com.zhbs.account.controller;

import cn.com.zhbs.account.entitys.Account;
import cn.com.zhbs.account.service.MenuService;
import cn.com.zhbs.common.Result.SimpleFactory.ResultFactory;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @program: vr_manage
 * @description: 菜单管理
 * @author: 陈治杰
 * @create: 2021-08-08 13:10
 **/
@Controller
@RequestMapping("menu")
public class MenuController {
    @Resource
    MenuService menuService;

    @RequestMapping(value = "menuList",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> menuList(@RequestBody JSONObject jsonObject,HttpServletRequest request){
        HttpSession session=request.getSession();
        try{
            jsonObject.put(Account.aid.toString(),session.getAttribute(Account.aid.toString()));
            jsonObject.put(Account.superRole.toString(),session.getAttribute(Account.superRole.toString()));
            return ResultFactory.success(menuService.menuList(jsonObject)).display();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultFactory.failure().display();
    }


    @RequestMapping(value = "addMenu",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> addMenu(@RequestBody JSONObject jsonObject, HttpServletRequest request){
        HttpSession session=request.getSession();
        try{
            jsonObject.put(Account.aid.toString(),session.getAttribute(Account.aid.toString()));
            return menuService.addMenu(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            return ResultFactory.failure().display();
        }
    }

    @RequestMapping(value = "editMenu",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> editMenu(@RequestBody JSONObject jsonObject, HttpServletRequest request){
        HttpSession session=request.getSession();
        try{
            jsonObject.put(Account.aid.toString(),session.getAttribute(Account.aid.toString()));
            menuService.editMenu(jsonObject);
            return ResultFactory.success().display();
        }catch (Exception e){
            e.printStackTrace();
            return ResultFactory.failure().display();
        }
    }

    @RequestMapping(value = "menuDetail",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> menuDetail(@RequestBody JSONObject jsonObject,HttpServletRequest request){
        HttpSession session=request.getSession();
        try{
            jsonObject.put(Account.aid.toString(),session.getAttribute(Account.aid.toString()));
            jsonObject.put(Account.superRole.toString(),session.getAttribute(Account.superRole.toString()));
            Map<String,Object> map=menuService.menuDetail(jsonObject);
            return ResultFactory.success(map).display();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultFactory.failure().display();
    }
}
